/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

/*
 * Dict.java
 * Created on January 20, 2008, 12:22 PM
 */

package tictionary.dict;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Erol Hira
 */
public class Dict implements Constants{
    
    private LetterSet rootSet;
    
    private Letter selfLetter;
    
    public Dict() {
        rootSet = new LetterSet(null);
    }
    
    public Dict(LetterSet rootSet){
        this.rootSet = rootSet;
    }

    public LetterSet getRootSet() {
        return rootSet;
    }
    
    //---------------------------------------------------------------------
    
    public Letter addToTree(String word){
        
        Letter letter = null;
        LetterSet letterSet = rootSet;
        
        for(int i = 0; i < word.length(); i++){
            letter = letterSet.forcedGiveLetter(word.charAt(i));
            
            if(i != word.length()-1){
                letterSet = letter.forcedGiveChildSet();
            }
        }        
        
        return letter;
    }
    
    public void addWord(String word, String mean){
        
        if(Character.isLetter(word.charAt(0)) && (Character.isDigit(word.charAt(0)) == false)){
            Letter w = addToTree(word);
            w.setMean(mean);
        }                
    }
    
    //---------------------------------------------------------------------
    
    public List<Dict> split(){
        
        ArrayList<Dict> parts = new ArrayList<Dict>();
        
        for(int i = 0; i < rootSet.getLetterCount(); i++){
            
            Letter letter = rootSet.getLetter(i);
            
            Dict dict = new Dict(letter.getChildSet());
            dict.setSelfLetter(letter);

            parts.add(dict);
        }
        
        return parts;
    }
    
    public String getDictLetter(){
        
        char ch = selfLetter.getLetter();
        
        if(ch == c_cedilla.charAt(0)){
            return "cc";
        } else if(ch == i_diaresis.charAt(0)){
            return "ii";
        } else if(ch == o_diaresis.charAt(0)){
            return "oo";
        } else if(ch == s_cedilla.charAt(0)){
            return "ss";
        } else if(ch == u_diaresis.charAt(0)){
            return "uu";
        } else {
            return Character.toString(ch);
        }
    }
    
    public String getDictFile(){
        return "out/".concat(getDictLetter().concat(".dat"));
    }

    //---------------------------------------------------------------------
    
    public Letter getSelfLetter() {
        return selfLetter;
    }

    public void setSelfLetter(Letter selfLetter) {
        this.selfLetter = selfLetter;
    }
    
    //---------------------------------------------------------------------
    
    public void compress(){
        traverseLeavesAndCompress(rootSet);
    }
        
    private void traverseLeavesAndCompress(LetterSet letterSet){                                
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            LetterSet childSet = letter.getChildSet();
            
            if(childSet == null){
                compressLettersFromLeaf(letter);
                continue;
            }
            
            traverseLeavesAndCompress(childSet);
        }
    }
    
    private void compressLettersFromLeaf(Letter letter){
        
        Letter tmpLetter = letter;
        
        StringBuilder buf = new StringBuilder();
        
        while(true){
            buf.append(tmpLetter.getLetter());
            if(tmpLetter.getSelfSet().getLetterCount() > 1){
                tmpLetter.setNoChildSet();
                break;
            } else {
                tmpLetter = tmpLetter.getSelfSet().getParentLetter();
            }
        }
        
        
        String remainder = StringUtils.reverse(buf.toString());
        tmpLetter.setChars(remainder);
    }
}
